<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">运营数据</label>
                        <div class="layui-input-block">
                            <a type="button" class="layui-btn layui-btn-primary layui-btn-radius" onclick="select_manage()" id="select_manage_data">
                                <i class="layui-icon">&#xe624;</i>选择运营数据
                            </a>
                            <div class="manage_html_append"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>

            let manage_data=[];

            // 相关接口
            const INSERT_API = "/app/admin/manage/index/insert";
            const SELECT_URL = "/app/admin/novel/index/selected?callback=search_manage_list";

            // 字段 图片地址 icon
            layui.use(["upload", "layer"], function() {
                let input = layui.$("#icon").prev();
                input.prev().attr("src", input.val());
                layui.$("#attachment-choose-image_path").on("click", function() {
                    parent.layer.open({
                        type: 2,
                        title: "选择附件",
                        content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                        area: ["95%", "90%"],
                        success: function (layero, index) {
                            parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                input.val(data.url).prev().attr("src", data.url);
                            });
                        }
                    });
                });

                layui.upload.render({
                    elem: "#icon",
                    value: "",
                    acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                    url: "/app/admin/upload/image",
                    field: "__file__",
                    headers: get_header({}),
                    done: function (res) {
                        if (res.code > 0) return layui.layer.msg(res.msg);
                        this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.on("submit(save)", function (data) {
                    data.field.manage_data = manage_data;
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });


            // 表格渲染
            layui.use([ "common", "popup", "util", 'tab', ], function() {
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;

                window.select_manage = function () {
                    callback = 'home_manage_list';
                    layer.open({
                        type: 2,
                        title: "选择",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"95%"],
                        content: SELECT_URL
                    });
                }
            });

            function search_manage_list(data) {
                var temp_data = [];

                var keyid = 'id';
                $.each(data,function (i,v) {
                    var ishas = false;
                    $.each(manage_data,function (ii,vv) {
                        if(vv && vv.id==v[keyid]){
                            ishas = true;
                        }
                    })

                    //过滤掉重复组件
                    if(ishas===false){
                        temp_data.push({image:v.main_image, id:v[keyid], name:v.title})
                    }
                })

                manage_data = $.merge(temp_data,manage_data);
                manage_render();
            }

            function manage_render() {
                var elemt_class = 'manage_html_append';

                var html = '';
                if(manage_data.length>0){
                    html += ' <fieldset class="table-search-fieldset" style="margin-top: 10px;border: 1px solid #e6e6e6;color: #6b6b6b;">\n';
                    html += '   <div class="wrapper"  style="display: flex;flex-direction:row;flex-wrap: wrap;">\n';
                    $.each(manage_data, function (i,v) {
                        html +=  '    <div class="app_icon_parent" style="height: 120px;display: flex;flex-direction:column;"draggable="true"\n' +
                            '            ondragstart="onDragStart(event)"\n' +
                            '            ondragover="onDragOver(event)"\n' +
                            '            ondrop="onDrop(event,'+upload_images_draggable+',\'.wrapper\')">\n';

                        html += '          <div style="height: 100px; width: 115px">';
                        html += '               <img src="'+v.image+'" data-name="'+v.name+'" data-id="'+v.id+'" onclick="open_image(this)" style="width: 100px;height: 100px; border: 1px solid #d3d1d1">\n';
                        html += '               <i class="layui-icon layui-icon-delete btn-delete-preview-image" data-id="'+v.id+'" onclick="delete_manage(this);event.cancelBubble =true;" style="color: red; font-size: 25px;position: relative;top: -105px;left: 80px;"></i>';
                        html += '           </div>'
                        html += '           <div style="width: 100px;font-size:12px;line-height:25px; text-align: center;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">'+v.name+'</div>';
                        html += '          \n';
                        html +=    '  </div>\n';
                    })

                    html +='    </div>';
                    html +='</fieldset>';
                }
                $('.'+elemt_class).html(html);
            }

            //拖动后，回调函数，重新组合数据
            function upload_images_draggable(parentCalss) {
                manage_data = [];
                $(parentCalss).find('.app_icon_parent').each(function (i,v) {
                    manage_data.push({image:$(v).find('img').attr('src'),name:$(v).find('img').data('name'),
                        id:$(v).find('img').data('id')});
                })
                manage_render();
            }

            function delete_manage($this) {
                var id = $($this).data('id');

                $.each(manage_data, function (i,v) {
                    if(v && id==v.id){
                        manage_data.splice(i,1);
                    }
                })
                manage_render();
            }
        </script>

    </body>
</html>
